package com.winenine999.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.model.Franchisee;
import com.winenine999.model.Page;
import com.winenine999.model.Purchase;
import com.winenine999.service.IPurchaseService;

@Controller
@RequestMapping(value = "/purchase")
public class PurchaseController {
	@Autowired
	IPurchaseService purchaseService;
	@RequestMapping(value="/in",method=RequestMethod.GET)
	public String index(){
		return "common/main";
	}
	/**
	 * 进入list
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(){
	
		return "message/purchase";	
	}
	/**
	 * 分页查询
	 * 一条sql。
	 * @param start
	 * @param length
	 * @param total
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=purchaseService.findPagePurchase(page);
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	/**
	 * 添加
	 * @param name
	 * @param endtime
	 * @param starttime
	 * @param detail
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Integer add(String name,String endtime,String starttime,String detail,Integer state){
		
		System.out.println(name);
		
		Purchase purchase=new Purchase();
		purchase.setName(name);
		purchase.setEndtime(endtime);
		purchase.setStarttime(starttime);
		purchase.setDetail(detail);
		purchase.setState(state);
		
		int num=purchaseService.insertSelective(purchase);
		
		return num;
	}
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findbyid")
	@ResponseBody
	public Purchase findbyid(int id){
		System.out.println(id);
		
		
		
		Purchase purchase=purchaseService.selectByPrimaryKey(id);
		
		return purchase;
	}
	/**
	 * 修改
	 * @param purchase
	 * @return
	 */
	@RequestMapping(value = "/updatepurchase")
	@ResponseBody
	public Integer updatepurchase(Purchase purchase) {
		
		Map<String,Object> map =new HashMap<>();
		Integer num=purchaseService.updateByPrimaryKeySelective(purchase);
		
		return num;
	}
	@RequestMapping(value = "/deletebyid")
	@ResponseBody
	public Integer deletebyid(Integer id){
		Map<String,Object> map =new HashMap<>();
		Integer num=purchaseService.deleteByPrimaryKey(id);
		return num;
	}
	@RequestMapping(value = "/HELLO",method=RequestMethod.GET)
	public String showPurchaseSub(Integer phid){
	
		return "redirect:/purchasesub/index?phid="+phid;
	}
}
